import json

import httpx
from loguru import logger

from .http import client

SIMPLE_PUSH_URL = "https://wxpusher.zjiecode.com/api/send/message/simple-push"
SIMPLE_PUSH_TOKEN = "SPT_RY73qT3690sF0WpbZbdDAe5FCj30"


def push(summary: str, content: str):
	input_data = {"content": content, "summary": summary, "contentType": 1, "spt": SIMPLE_PUSH_TOKEN}
	response = httpx.post(SIMPLE_PUSH_URL, json=input_data)
	content = str(response.content, encoding="utf-8")
	logger.info("content = {}", content)
	response.close()


async def async_push(summary: str, content: str):
	input_data = {"content": content, "summary": summary, "contentType": 1, "spt": SIMPLE_PUSH_TOKEN}
	logger.info("input_data = {}", input_data)
	response = await client.post(SIMPLE_PUSH_URL, json=input_data)
	content = str(response.content, encoding="utf-8")
	logger.info("content = {}", content)
	await response.aclose()


def search_uid():
	url = "https://wxpusher.zjiecode.com/api/fun/wxuser/v2"
	response = httpx.get(url, params={"appToken": "AT_WP0mxEUjJePMffWOz5jEwXt3pdBvAEus", "page ": 1, "pageSize": 100, "type": 0})
	content = str(response.content, encoding="utf-8")
	logger.info("content = {}", content)
	res = json.loads(content)
	response.close()
	return [item["uid"] for item in res["data"]["records"]]


def push_multi(summary: str):
	token = "AT_WP0mxEUjJePMffWOz5jEwXt3pdBvAEus"
	url = "https://wxpusher.zjiecode.com/api/send/message"
	input_data = {"appToken": token, "content": "打卡消息推送", "summary": summary, "contentType": 1, "uids": search_uid(), "verifyPayType": 0}
	response = httpx.post(url, json=input_data)
	content = str(response.content, encoding="utf-8")
	logger.info("content = {}", content)
	response.close()
